package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.domain.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.member.util.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * redis数据初始化监听器
 * ContextRefreshedEvent:容器启动或者重新加载触发的事件
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {


    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    //当容器启动时调用该方法
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("-------------vo初始化-start--------------");
        //将攻略数据初始化到redis中的vo对象中
        //获取所有攻略数据
        List<StrategyDetail> list = strategyDetailService.list();
        StrategyStatisVO vo = null;
        for (StrategyDetail detail : list) {
            //解决数据丢失问题,多次初始化,会将前端操作没来及持久化的数据覆盖,导致数据丢失,如果数据存在的话则跳过
            if (strategyStatisRedisService.isStraisVoExist(detail.getId())) {
                continue;
            }
            //遍历所有攻略,然后创建vo对象,然后进行属性拷贝,然后拷贝不了的自己手动拷贝
            vo = new StrategyStatisVO();
            BeanUtil.copyProperty(vo, detail);
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setStrategyId(detail.getId());
            //然后将数据同步到redis中
            strategyStatisRedisService.setStrategyStatisVO(vo);

        }
        System.out.println("-------------vo初始化-end--------------");
        System.out.println("-------------hot推荐初始化-start--------------");

        for(StrategyDetail detail : list){
            if (strategyStatisRedisService.isZsetStraisVoExist(RedisKeys.STRATEGY_ZEST_SORT_HOT.getPrefix(), detail.getId())) {
                continue;
            }
            //阅读数跟收藏数
            int score = detail.getViewnum() + detail.getReplynum();
            strategyStatisRedisService.addHotScore(detail.getId(), 1);

        }


        System.out.println("-------------hot推荐初始化-end--------------");

        System.out.println("-------------国内外初始化-start--------------");
        for (StrategyDetail detail : list) {
            /**
             * 统计阅读数和评论数
             * 进行初始化操作是,需要检验,如果不检验的话会导致重复加分问题
             * 比如:第一次启动100分,  页面进行操作变成200分了,第二次启动,如果不检验的话,就变成300分了
             */
            if (strategyStatisRedisService.isZsetStraisVoExist(RedisKeys.STRATEGY_ZSET_SORT_COMMEND.getPrefix(), detail.getId())) {
                continue;
            }
            //点赞数跟收藏数
            int score = detail.getThumbsupnum() + detail.getFavornum();
            strategyStatisRedisService.addCommendScore(detail.getId(), 1);
        }
        System.out.println("-------------国内外初始化-end--------------");
    }
}
